﻿/*-----------------------------------
*用于定义一些跟按钮相关的方法
* 
*/
//新增
var fnAdd = function () {
    if (Y.util.Dom.getViewportHeight() < 400 || Y.util.Dom.getViewportWidth() < 400) {
        SetMaxDWH(YAHOO.example.container.dialog1);
    }
    if (typeof (fnCustomAdd) == "function") {
        fnCustomAdd();
        //return;
    } else {
        if (document.getElementById("yui-gen7-button") != null) {
            document.getElementById("yui-gen7-button").style.display = "";
        }
        //var pageFile = editpageFlag == 0 ? "AutoEditUI.aspx" : "AutoDTEditUI.aspx";
        if (editpageFlag == 0) {
            document.getElementById("EditUIWindow").src = "AutoEditUI.aspx?r=" + Math.random() + "&entity=" + const_EditEntity; // pageFile + "?r=" + Math.random() + "&entity=" + const_EditEntity;
        }
        else {
            document.getElementById("EditUIWindow").src = "AutoDTEditUI.aspx?r=" + Math.random() + "&entity=" + const_EditEntity;
        }
        YAHOO.example.container.dialog1.show();
        //SetMaxDWH(YAHOO.example.container.dialog1);
    }
}

var fnAddMasterDetail = function () {
    if (typeof (fnCustomAddMasterDetail) == "function") { 
        fnCustomAddMasterDetail();}
    else {
        var url = "Extend/eMasterDetail.aspx?r=" + Math.random() + "&entity=" + const_EditEntity + "&subEntityName=EmpGoods";
        parent.addTabs('编辑', url);
    }
}
//-------------------------------------------
//更新数据到DataTable
//-------------------------------------------
var fnUpdateRow = function (obj) {
    if (typeof (fnCustomUpdateRow) == "function") {
        fnCustomUpdateRow();
        return;
    } else {
        var recordID = mDataTable.getSelectedRows()[0];
        mDataTable.updateRows(recordID, obj);
    }
}

//-------------------------------------------
//新增行
//-------------------------------------------
var fnAddRow = function (obj) {
    if (typeof (fnCustomAddRow) == "function") {
        fnCustomAddRow();
        return;
    } else {
        mDataTable.addRow(obj, 0);
    }
}

//------------------------------------------
// 重新加载数据
//------------------------------------------
var fnReload = function () {
    if (typeof (fnCustomReload) == "function") {
        fnCustomReload();
        return;
    } else {
        const_CustomCondition = "";
        //alert(this.location.href);
        RefreshData();
    }
}

//删除
function DeleteData() {
    if (typeof (fnCustomDeleteData) == "function") {
        fnCustomDeleteData();
        return;
    } else {

        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;

                    if (str == "1") {
                        alert("数据删除成功");
                        var recordID = mDataTable.getSelectedRows()[0];
                        if (dl.length == 0) {
                            mDataTable.deleteRow(recordID);
                        }
                        else {
                            for (var i = 0; i < dl.length; i++) {
                                mDataTable.deleteRow(dl[i]);
                            }
                        }
                    }
                    else {
                        alert(str + "\n" + "数据删除失败!");
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
        YAHOO.example.container.dialog3.hide();
    }
}

// -------------------------------------
//全屏功能
//--------------------------------------
var fnFull = function () {
    if (parent.fullParentObj == null) {
        //this.value = "退出全屏";
        document.getElementById("btnFull-button").innerHTML = "退出全屏";
        parent.fullScreen(window.frameElement);
        document.getElementById("btnClose-button").style.display = "none";
    }
    else {
        //alert("-2-")
        document.getElementById("btnFull-button").innerHTML = "全屏";
        parent.unfullScreen();
        document.getElementById("btnClose-button").style.display = "block";
    }
}

//弹出帮助信息,functionId:菜单ID
var fnModuleHelp = function () {
    //获取当前模块菜单ID
    var funId = getPar1("functionID");
    var pagetitle = "帮助";
    var url = "../Extend/AjaxData/jsonlist.aspx?entity=SysModuleHelp&condition=FunctionID='" + funId + "'&r=" + Math.random();
    $.get(url, function (d, s) {
        var list = eval("(" + d.toString().replace(/\\"/g, '"') + ")");
        if (list.total > 0) {
            for (var o in list.rows) {
                var v = list.rows[o].Title;
                if (typeof (v) != "undefined") {
                    pagetitle = v;
                    parent.addTabs(pagetitle, "/EHR/ShowHelp.aspx?functionId=" + funId + "&r=" + Math.random());
                    parent.NeedRefreshPage = this;
                    break;
                }
            }
        }
    });
}

/// 
var fnListSetting = function () {
    //获取当前模块菜单ID
    if (document.getElementById("yui-gen7-button") != null) {
        document.getElementById("yui-gen7-button").style.display = "";
    }
    document.getElementById("EditUIWindow").src = "YUILayout.aspx?functionid=" + const_functionId + "&r=" + Math.random() + "&table=" + const_entityName + "&entity=" + const_entityName;
    //SetMaxDWH(YAHOO.example.container.dialog1);
    YAHOO.example.container.dialog1.show();
}
//----------------------------------
//数据删除操作
//----------------------------------
var fnDelete = function () {
    if (typeof (EditEntity) != "undefined") {
        const_EditEntity = EditEntity;
    }
    if (typeof (fnCustomDelete) == "function") {
        fnCustomDelete();
        return;
    } else {
        var list = this.mDataTable.getRecordSet()._records;
        var elist = [];
        dl = [];
        for (var i = 0; i < list.length; i++) 
        {
            //this.getRecordSet()._records[0]._oData["BarItem"]
            if (list[i]._oData["iSun_Selected"] == true) {
                elist.push(list[i]._oData[const_entityID]);
                dl.push(list[i]._sId); //获取行ID；
            }
        }
        if (elist.length == 0) {
            var recordID = myDataTable.getSelectedRows()[0];
            record = myDataTable.getRecord(recordID);
            if (editpageFlag == 0) {
                SaveUrl = "AjaxData/DataSave.aspx?r=" + Math.random() + "&action=delete&entity=" + const_EditEntity + "&id=" + record.getData(const_entityID);
            } else {
                SaveUrl = "AjaxData/DataSave.aspx?r=" + Math.random() + "&action=delete&table=" + const_EditEntity + "&id=" + record.getData(const_entityID);
            }
        }
        else {
            if (editpageFlag == 0) {
                SaveUrl = "AjaxData/DataSave.aspx?r=" + Math.random() + "&action=delete&entity=" + const_EditEntity + "&json=" + elist.join(",");
            }
            else {
                SaveUrl = "AjaxData/DataSave.aspx?r=" + Math.random() + "&action=delete&table=" + const_EditEntity + "&json=" + elist.join(",");
            }
        }
        YAHOO.example.container.dialog3.show();
        //DeleteData("AjaxData/DataSave.aspx?r=" + Math.random()+"&action=delete&entity="+const_EditEntity+"&id="+record.getData(const_entityID),LoadData);           
    }
}


//-----------------------------------------
//编辑
//-----------------------------------------
var fnEdit = function (key) {
    if (Y.util.Dom.getViewportHeight() < 401) {
        //alert(Y.util.Dom.getViewportHeight());
        SetMaxDWH(YAHOO.example.container.dialog1);
    }
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    if (typeof (fnCustomEdit) == "function") {
        fnCustomEdit();
        if (key != undefined) {
            document.getElementById("yui-gen7-button").style.display = "none";
        }
        return;
    } else {
        if (editpageFlag == 0) {
            document.getElementById("EditUIWindow").src = "AutoEditUI.aspx?r=" + Math.random() + "&entity=" + const_EditEntity + "&id=" + record.getData(const_entityID);
        }
        else {
            document.getElementById("EditUIWindow").src = "AutoDTEditUI.aspx?r=" + Math.random() + "&entity=" + const_EditEntity + "&id=" + record.getData(const_entityID);
        }
        if (document.getElementById("yui-gen7-button") != null) {
            document.getElementById("yui-gen7-button").style.display = "";
        }
        if (key != undefined) {
            document.getElementById("yui-gen7-button").style.display = "none";
        }
        YAHOO.example.container.dialog1.show();
    }
}

//
var fnView = function () {
    if (typeof (fnCustomView) == "function") {
        fnCustomView();
        return;
    } else {
        var recordID = myDataTable.getSelectedRows()[0];
        record = myDataTable.getRecord(recordID);
        document.getElementById("EditUIWindow").src = "AutoEditUI.aspx?action=view&r=" + Math.random() + "&entity=" + const_EditEntity + "&id=" + record.getData(const_entityID);
        document.getElementById("yui-gen7-button").style.display = "none";
        YAHOO.example.container.dialog1.show();
        var editwindow = document.getElementById("EditUIWindow");
        if (editwindow) {
            setTimeout(editwindow.contentWindow.$("#form1").SeControlReadOnly(), 200);
        }
    }
}

//------------------------------------------
//查询
//------------------------------------------
var fnQuery = function () {    
    if (typeof (fnCustomQuery) == "function") {
        fnCustomQuery();
        return;
    } else {
        document.getElementById("QueryWindow").src = "QueryForm.aspx?r=" + Math.random() + "&entity=" + const_entityName + "&functionid=" + const_functionId;
        YAHOO.example.container.dialog4.show();
    }
}

//设置字段
var fnSetField = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    document.getElementById("QueryWindow").src = "TableManager.aspx?tableid=" + record.getData("TableID") + "&table=" + record.getData("TableName") + "&r=" + Math.random() + "&entity=" + const_entityName + "&functionid=" + const_functionId;
    YAHOO.example.container.dialog4.show();
}

//-------------------------------------------
//查询执行
//-------------------------------------------

var fnQueryRe = function (condition) {
    
    //alert("");
    if (typeof (fnCustomQueryRe) == "function") {
        fnCustomQueryRe(condition);
        return;
    } else {
        const_CustomCondition = condition;
        RefreshData();
    }
}

///-------------------------------------------
///快速搜索
///-------------------------------------------
var fnQuciklyQuery = function () {
    
    if (const_QueryField == undefined || const_QueryField == "") {
        return;
    }
    var _keyword = document.getElementById("QueryContext").value;
    if (_keyword == "") {
        return;
    }
    var _chk = document.getElementById("checkAndOr").checked;
    //alert(_chk);
    var _opr = _chk ? " and " : " or ";
    //alert(_opr);
    var _fields = const_QueryField.split(",");
    var _keys = _keyword.split(" ");
    var _condition = "";
    for (n = 0; n < _keys.length; n++) {
        if (_keys[n] == "") {
            continue;
        }
        if (n == 0) {
            _condition = " ( "
        }
        else {
            _condition = _condition + _opr + " ( ";
        }
        for (i = 0; i < _fields.length; i++) {
            if (i == 0) {
                _condition = _condition + " (" + _fields[i] + " like '%" + _keys[n] + "%')  ";
            }
            else {
                _condition = _condition + " or (" + _fields[i] + " like '%" + _keys[n] + "%')  "
            }
        }
        _condition = _condition + " )";
    }
    //alert(_condition);
    fnQueryRe(encodeURI(_condition));
}
//-------------------------------------------
//导出
//-------------------------------------------
var fnExport = function () {
    if (typeof (fnCustomExport) == "function") {
        fnCustomExport();
        return;
    } else {
        if (this.location.search.indexOf('condition') < 0) {
            parent.addTabs("报表/导出", "/Extend/AutoLoadReportView.aspx" + this.location.search + "&condition=" + const_Condition + "&customCondition=" + const_CustomCondition);
        } else {
            parent.addTabs("报表/导出", "/Extend/AutoLoadReportView.aspx" + this.location.search + "&customCondition=" + const_CustomCondition);
        } //var request = "AjaxData/DataProcessin.aspx?action=Import&entity=" + const_entityName;
        //window.open(request);
    }
}


//-------------------------------------------
//报表
//-------------------------------------------
var fnReport = function () {
    alert("");
    if (typeof (fnCustomReport) == "function") {
        fnCustomReport();
        return;
    }
    parent.addTabs("报表", "/Extend/AutoReportView.aspx" + this.location.search);
}


//-------------------------------------------
//获取当选的对象
//-------------------------------------------
var getSelectItem = function () {
    if (typeof (fnCustomgetSelectItem) == "function") {
        fnCustomgetSelectItem();
        return;
    } else {
        var recordID = myDataTable.getSelectedRows()[0];
        record = myDataTable.getRecord(recordID);
        return record;
    }
}
